package com.zc.remoteControl.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.zc.manager.device.entity.DeviceProperty;
import com.zc.remoteControl.service.RemoteInstructSendService;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import reactor.core.Disposable;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Author glb
 * @Date 2023/12/12 9:06
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "/remoteInstructSend")
public class RemoteInstructSendController {

    @Resource
    private RemoteInstructSendService remoteInstructSendService;//设备


    /**
     * 查询设备属性
     * @param deviceId 设备id
     * @return 获取属性数据操作结果
     */
    @GetMapping(value = "getRealData")
    @ResponseBody
    public AjaxResult getRealData(@Parameter(description = "设备ID") String deviceId){
        Map<String, DeviceProperty> realData = remoteInstructSendService.getRealData(deviceId);
        return AjaxResult.success(realData);
    }

    /**
     * 发送报文到设备
     * @param deviceId 设备id
     * @param functionId 属性id
     * @param props 调用参数
     * @return 获取发送结果
     */
    @PostMapping(value = "sendMessage")
    @ResponseBody
    public AjaxResult sendMessage(@Parameter(description = "设备ID")String deviceId,@Parameter(description = "属性ID")String functionId,@RequestBody Map<String, Object> props){
        Disposable disposable = remoteInstructSendService.sendMessage(deviceId, functionId, props);
        return AjaxResult.success(disposable);
    }

    /**
     * 发送报文到设备
     * @param deviceId 设备id
     * @param functionId 属性id
     * @param props 调用参数
     * @return 获取发送结果
     */
    @PostMapping("/functions/{deviceId}/{functionId}")
    public AjaxResult invokedFunction(
            @PathVariable String deviceId,
            @PathVariable String functionId,
            @RequestBody Map<String, Object> props) {
        Disposable disposable = remoteInstructSendService.sendMessage(deviceId, functionId, props);
        return AjaxResult.success(disposable);
    }
}
